package com.king01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.king01.dao.BranchDao;
import com.king01.dao.Branch_goodsDao;
import com.king01.entity.Branch_goods;
import com.king01.entity.Goods;
import com.king01.entity.Inlibrary;
import com.king01.entity.Staff;
import com.king01.service.Branch_goodsService;
import com.king01.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author ：lqs
 * @date ：Created in 2023/9/27 14:54
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class Branch_goodsServiceImpl implements Branch_goodsService {
    @Autowired
    private Branch_goodsDao branch_goodsDao;
    @Autowired
    HttpSession session;

    @Override
    public Boolean selectById(List<Branch_goods> list) {
        //获取session中的消息
        Staff staff = (Staff) session.getAttribute("staff");
        for (int i = 0; i < list.size(); i++) {
            List<Branch_goods> branch_goods = branch_goodsDao.selectById(list.get(i).getGoodsid(),staff.getStaffid(),staff.getBranchid());
            if (branch_goods.size()!=0){
                return true;
            }
        }
        return false;
    }

    @Override
    public Result addGoods(List<Branch_goods> list) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer integer = branch_goodsDao.addGoods(list,staff.getBranchid().toString());
        return new Result(200,"成功",integer);
    }


    @Override
    public Result selectByNewAdd(Integer currentPage, Integer pageSize) {
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage,pageSize);
        Staff staff = (Staff) session.getAttribute("staff");
        List<Branch_goods> branch_goods = branch_goodsDao.selectByNewAdd(staff.getBranchid());
        //把查询的结果封装PageInfo对象中
        PageInfo<Branch_goods> pageInfo=new PageInfo<>(branch_goods);
        return new Result(200,"添加入库",pageInfo);
    }

    @Override
    public Result addBranchGoods(List<AddBranchDoodsVo> addBranchDoodsVo) {

        Staff staff = (Staff) session.getAttribute("staff");

        Date date = new Date();
        addBranchDoodsVo.get(0).setDate(date);
        addBranchDoodsVo.get(0).setStaffid(staff.getStaffid());
        AddBranchDoodsVo addBranchDoodsVo1 = addBranchDoodsVo.get(0);
        Integer integer = branch_goodsDao.addBranchGoods(addBranchDoodsVo1);

        //获取刚刚加入的id
        List<AddBranchDoodsVo> addBranchDoodsVos = branch_goodsDao.maxId();
        for (AddBranchDoodsVo branchDoodsVo : addBranchDoodsVo) {
            branchDoodsVo.setId(staff.getStaffid());
            branchDoodsVo.setId(addBranchDoodsVos.get(0).getId());
            branchDoodsVo.setDate(date);
        }
        //在storage_volume中插入数据
        Integer integer1 = branch_goodsDao.addByStorage_volume(addBranchDoodsVo);


        return new Result(200,"入库成功",integer1);
    }
    //查询详细信息的数据
    @Override
    public Result selectAll(Integer id) {
        List<Storage_volumeVo> storage_volumeVos = branch_goodsDao.selectAll(id);
        for (Storage_volumeVo storage_volumeVo : storage_volumeVos) {
            Integer cprice = storage_volumeVo.getCprice();
            Integer num = storage_volumeVo.getNum();
            storage_volumeVo.setAllprice(cprice*num);
        }

        return new Result(200,"查询详细信息成功",storage_volumeVos);
    }
    //先查询全部信息
    @Override
    public Result selectAll2(Integer id) {
        List<Storage_volumeVo> storage_volumeVos = branch_goodsDao.selectAll(id);
        for (Storage_volumeVo storage_volumeVo : storage_volumeVos) {
            //   获取商品的id
            Integer goodsid = storage_volumeVo.getGoodsid();
            Branch_goods byBG = branch_goodsDao.findByBG(storage_volumeVo.getBranchid(), storage_volumeVo.getGoodsid());
            //获取入库量
            Integer num = storage_volumeVo.getNum();
            //得出总数
            Integer i = byBG.getCounts() + num;
            //修改branch_goods中的数据
            Integer integer1 = branch_goodsDao.updateByBG(storage_volumeVo.getBranchid(), storage_volumeVo.getGoodsid(),i);
        }
        return new Result(200,"修改成功",null);
    }

    @Override
    public Result addOutLibrary(Integer currentPage, Integer pageSize,BranchVo branchVo) {
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage,pageSize);
        Staff staff = (Staff) session.getAttribute("staff");
//        if (staff.getStaffid()!=111){
//            branchVo.setBranchid(staff.getBranchid());
//        }
        List<AddOutLibraryVo> addOutLibraryVos = branch_goodsDao.addOutLibrary(staff.getBranchid());
        PageInfo<AddOutLibraryVo> pageInfo=new PageInfo<>(addOutLibraryVos);
        return new Result(200,"出库",pageInfo);
    }


}

